package zhklappoint.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import zhklappoint.annotation.IgnoreAuth;
import zhklappoint.annotation.LoginUser;
import zhklappoint.entity.Company;
import zhklappoint.entity.FunctionAndCompany;
import zhklappoint.entity.FunctionKinds;
import zhklappoint.entity.WXUser;
import zhklappoint.enums.ENUM_FUNCTION_IMG;
import zhklappoint.enums.ENUM_ISUSING;
import zhklappoint.enums.ENUM_NORMAL;
import zhklappoint.enums.ENUM_SUCESS;
import zhklappoint.service.FunctionAndCompanyService;
import zhklappoint.service.FunctionKindsService;
import zhklappoint.service.InsertIntoCompanyInfoService;
import zhklappoint.util.ResponseBean;
import zhklappoint.util.Tools;


@Controller
@CrossOrigin
@RequestMapping(value="/uni/api",produces = "text/html;charset=UTF-8")
public class CompanyController {
	
	
	@Autowired
	private InsertIntoCompanyInfoService ics;
	@Autowired
	private FunctionKindsService fks;
	@Autowired
	private FunctionAndCompanyService facs;
	
	//用户申请使用填写基础资料
			@RequestMapping(value="/addCompany",produces = "application/x-www-form-urlencoded;charset=UTF-8",method=RequestMethod.POST)
			@ResponseBody
			public String addCompany(Company company,@LoginUser WXUser user) {
				Company c = new Company();
				if(ObjectUtils.isEmpty(company.getBossPhome())||!Tools.isPhoneLegal(company.getBossPhome())) {
					return ResponseBean.of(null, ENUM_SUCESS.FAILED.value, "联系电话格式错误");
				}
				c.setWxUserId(user.getId());
				Page<Company> pcy = ics.getCompanyList(c, 1, 10);
				if(pcy.getTotalElements()>0&&ObjectUtils.isEmpty(company.getId())) {
					return ResponseBean.of(null, ENUM_SUCESS.FAILED.value, "您已注册了公司");
				}
				c.setWxUserId(null);
				c.setBossPhome(company.getBossPhome());
				Page<Company> pcys = ics.getCompanyList(c, 1, 10);
				if(pcys.getTotalElements()>0) {
					if((pcys.getTotalElements()+"").equals("1")&&!ObjectUtils.isEmpty(company.getId())) {
						System.out.println((pcys.getTotalElements()+"").equals("1")+"----"+ObjectUtils.isEmpty(company.getWxUserId()));
					}else {
						return ResponseBean.of(null, ENUM_SUCESS.FAILED.value, "当前电话号码:"+company.getBossPhome()+"已被注册");
					}
				}
				if(ObjectUtils.isEmpty(company.getCompanyName())) {
					return ResponseBean.of(null, ENUM_SUCESS.FAILED.value, "单位名称不能为空");
				}
				c.setBossPhome(null);
				c.setCompanyName(company.getCompanyName());
				pcys = ics.getCompanyList(c, 1, 10);
				if(pcys.getTotalElements()>0) {
					if(pcys.getTotalElements()==1&&!ObjectUtils.isEmpty(company.getId())) {
					}else {
						return ResponseBean.of(null, ENUM_SUCESS.FAILED.value, "名称为:"+company.getCompanyName()+"的单位已被注册");
					}
				}
				if(ObjectUtils.isEmpty(company.getBossName())) {
					return ResponseBean.of(null, ENUM_SUCESS.FAILED.value, "联系人名不能为空");
				}
				if(Tools.isNumeric(company.getBossName())) {
					return ResponseBean.of(null, ENUM_SUCESS.FAILED.value, "联系人名必须是汉字");
				}
				if(ObjectUtils.isEmpty(company.getCompanyIntr())) {
					company.setCompanyIntr("这个店主很懒，没做任何介绍");
				}
				if(!ObjectUtils.isEmpty(company.getSwitchImg())) {
					if(company.getSwitchImg().lastIndexOf(",")==company.getSwitchImg().length()-1) {
						company.setSwitchImg(company.getSwitchImg().substring(0,company.getSwitchImg().length()-1));
					}
				}
				company.setStatus(ENUM_NORMAL.ABNORMAL.value);
				company.setWxUserId(user.getId());
				Boolean b=false;
				if(ObjectUtils.isEmpty(company.getId())) {
					b=true;
				}
				company = ics.saveCompany(company);
				if(b) {
					FunctionKinds fk=new FunctionKinds();
					Page<FunctionKinds> pfks = fks.getFunctionKindsList(fk, 1, 100);
					for(FunctionKinds f:pfks.getContent()) {
						if(f.getCode()<3){//公司初始化只需要把公司简介  预约和我的预约几个基础功能开放
							FunctionAndCompany fac = new FunctionAndCompany();
							fac.setCompanyId(company.getId());
							fac.setFkId(f.getId());
							fac.setStatus(ENUM_ISUSING.USING.value);
							facs.saveFunctionAndCompanyObj(fac);
						}
					}
				}
				return ResponseBean.of(company.getId(), ENUM_SUCESS.SUCESS.value, "成功申请为合作单位");
			}
			
	//获取公司信息   店主进入他的店铺管理后台
    @RequestMapping(value="/getAdminCompany",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getAdminCompany(@LoginUser WXUser user) {
    	Company c = new Company();
    	c.setWxUserId(user.getId());
    	Page<Company> pcom = ics.getCompanyList(c, 1, 10);
    	Map<String,Object> maps = new HashMap<String,Object>();
    	//公司信息以及公司的图片域名地址
    	if(pcom.getTotalElements()>0) {
    		maps.put("com", pcom.getContent().get(0));
    	}else {
    		return ResponseBean.of(null,ENUM_SUCESS.FAILED.value , "系统还没建立呢");
    	}
    	maps.put("imgpath", Tools.splitString("backimg","indexpage",0));
    	//用户当前可以使用的对应功能TODO
    	
    	return ResponseBean.of(maps,ENUM_SUCESS.SUCESS.value , "获取成功");
    }
    //获取某个店铺的详细信息
	@IgnoreAuth
	@RequestMapping(value="/getPersonCompany",produces = "application/x-www-form-urlencoded;charset=UTF-8")
	@ResponseBody
	public String getPersonCompany(Company company) {
		Page<Company> pcs = ics.getCompanyList(company, 1, 1);
		if(pcs.getTotalElements()==0) {
			return ResponseBean.of(null, ENUM_SUCESS.FAILED.value, "信息有误");
		}
		List<Company> lcs = pcs.getContent();
		for(Company cany:lcs) {
			if(!ObjectUtils.isEmpty(cany.getImgUrl())) {//用户背景大图只有一张
				cany.setImgUrl(Tools.splitString(cany.getImgUrl(), "backimg","indexpage",0));
			}
			if(!ObjectUtils.isEmpty(cany.getLogoImage())) {//单位logo
				cany.setLogoImage(Tools.splitString(cany.getLogoImage(), "backimg","indexpage",0));
			}
			if(!ObjectUtils.isEmpty(cany.getImgUrlBack())) {
				cany.setImgUrlBack(Tools.splitString(cany.getImgUrlBack(), "backimg","indexpage",0));
			}
			if(!ObjectUtils.isEmpty(cany.getSwitchImg())) {
				String[] imgList=cany.getSwitchImg().split(",");
				String switchImgspath="";
				for(int i=0;i<imgList.length;i++) {
					if(i==imgList.length-1) {
						switchImgspath=switchImgspath+Tools.splitString(imgList[i], "backimg","indexpage",0);
					}else {
						switchImgspath=switchImgspath+Tools.splitString(imgList[i], "backimg","indexpage",0)+",";
					}
				}
				cany.setSwitchImg(switchImgspath);
			}
		}
		return ResponseBean.of(lcs, ENUM_SUCESS.SUCESS.value, "获取详情成功");
	}
	
	
}
